<?php
namespace app\workerman\controller;
use think\worker\Server;
use Workerman\Lib\Timer;
// use Workerman\Connection\AsyncTcpConnection;
/**
 * @author KEL
 * @Date   2019-07-29 11:17:53
 * @Last Modified by   KEL
 */
class Worker extends Server{
    protected $protocol = 'websocket';
    protected $host      = '';
    protected $port      = '';
    protected $socket = '';
    protected $option = [];

    public function __construct(){
        $this->host = '0.0.0.0';
        $this->port = '11000';
        $this->option['name'] = 'socket_serever';
        $this->option['count'] = 1;
        parent::__construct();
    }
    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data){
        echo "收到消息\n";
    }

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection){
        echo  "连接上了\n";
        echo "当前客户数：".count($connection->worker->connections)."\n";
        echo "连接上了\n";
    }

    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection){
        echo "关闭连接\n";
    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg){
        echo "出错了\n";
    }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker){
        echo "启动进程\n";
        Timer::add(5, function(){
            echo "运行获取短信定时器\n";
            $api_url = config('worker_server.timer_get_sms_url');
            $this->curl_post($api_url);
        });


        Timer::add(3, function(){
            echo "运行释放定时器\n";
            $api_url1 = config('worker_server.timer_denial_url');
            $this->curl_post($api_url1);
        });

        //20秒一次
        Timer::add(20, function(){
            //每天凌晨执行一次
            if(date('H:i',time())  == '00:00') {
                echo "升降级定时器\n";
                $api_url2 = config('worker_server.timer_sjj_url');
                $this->curl_post($api_url2);
            }
            
        });

        //获取手机号
        Timer::add(5, function(){
            echo "运行获取手机号定时器\n";
            $api_url = config('worker_server.timer_get_phone_url');
            $this->curl_post($api_url);
        });


    }

    public function onWorkerReload(){
        echo "重新载入\n";
    }

    //写入日志
    function sys_log($str='',$path='web', $file='proxy.txt'){
        $fileDir = date('Y-m-d',time());
        $fileDir = dirname(dirname(dirname(dirname(__FILE__)))).'/log/'.$fileDir.'/'.$path;
        // echo $fileDir."\n";
        //按小时拆分
        if(!is_dir($fileDir)){
            $this->mk_dir($fileDir);
        }
        $fileDir = $fileDir.'/'.date('H',time()).'/';
        if(!is_dir($fileDir)){
            $this->mk_dir($fileDir);
        }
        $ft = @fopen($fileDir.$file, 'a');
        @fwrite($ft,'执行时间:'.date('Y-m-d H:i:s',time()).' '.$str."\n");
        @fclose($ft);
    }

    // 循环创建目录
    function mk_dir( $dir, $mode = 0777 ){
        if( is_dir( $dir ) || @mkdir( $dir, $mode ) ) {
            return true;
        }
        if ( !self::mk_dir( dirname( $dir ), $mode ) ) {
            return false;
        }
        return @mkdir( $dir, $mode );
    }

    function curl_post($url , $data=[], $timeOut=0){
        $ch = curl_init(); 
        if($timeOut){
            //请求等待时间设置为  timeOut 毫秒,达到异步的效果
            curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeOut);
        }
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        $info = curl_exec($ch);
        curl_close($ch);
        return $info;
    }
}